#include <stdio.h>

#include "ebm_env.h"
#include "nstar_light_glint.h"

#include "user_cfg.h"

#if 0
extern unsigned char sys_status_logic_addr_get(unsigned char* logicBuf);
extern unsigned char sys_status_logic_addr_set(unsigned char* logicBuf);
extern unsigned char syscfg_flgAmplifier_get(void);
extern unsigned char syscfg_flgAmplifier_set(unsigned char val);
extern unsigned char syscfg_passback_sip_set(unsigned int sIp);
extern unsigned char syscfg_passback_port_set(unsigned short sPort);
extern unsigned short syscfg_command_tp_get(void);
extern unsigned char syscfg_command_tp_set(unsigned short val);
extern unsigned short syscfg_passback_tp_get(void);
extern unsigned char syscfg_passback_tp_set(unsigned short val);
extern unsigned char sys_status_pyhsics_addr_get(unsigned char* physicsBuf);

extern unsigned int sys_status_local_ip_get(void);
extern unsigned int sys_status_netmask_get(void);
extern unsigned int sys_status_gatway_get(void);
extern unsigned short sys_status_local_port_get(void);
extern unsigned char nstar_adt_local_ip_set(unsigned int ip,unsigned int netmask, \
	unsigned int gatway,unsigned short multiNum);
#endif


char* gp_oemManufactureid;
char* gp_oemHardwareVersion;
char* gp_oemSoftversion; 

unsigned short syscfg_plat_port_get(void) {return ReadCfg->ebm.plat_port;}
unsigned int syscfg_passback_sip_get(void) {return ReadCfg->ebm.passback_ip;}
unsigned short syscfg_passback_port_get(void) {return ReadCfg->ebm.passback_port;}


#if 0
void ebm_update_local_ip(void)
{
	unsigned char ret=0;
	unsigned int ip, netmask, gatway;
	unsigned short port;
	ip= sys_status_local_ip_get();
	netmask= sys_status_netmask_get();
	gatway= sys_status_gatway_get();
	port= sys_status_local_port_get();
	ret= nstar_adt_local_ip_set(ip, netmask, gatway, port);
	if(ret != 0){
		command_net_restart();
		passback_net_restart();
		rtp_net_restart();
	}
}
#else

void ebm_update_local_ip(void)
{
	user_reboot(3);//有BUG，直接重启
}

#endif



unsigned char ebm_env_get_vol(void) {return ReadCfg->vol;}
unsigned char ebm_env_get_volmax(void) {return ReadCfg->vol_max;}
unsigned char syscfg_fm_tp_get(void) {return ReadCfg->ebm.fm_tp;}
unsigned char ebm_get_phyid(unsigned char* physicsBuf) { memcpy(physicsBuf, read_serid(), 6);\
                                                                 return 1;}
unsigned int ebm_get_fmfq(void) {return ReadCfg->ebm.fm_fq;}
unsigned char ebm_set_fmfq(unsigned int val) { return set_val_u32(val, &p_wcfg->ebm.fm_fq); }


unsigned char ebm_env_set_vol(unsigned char vol)
{
extern void zxc_env_set_vol(unsigned char vol);
	unsigned char ret= usercfg_set_vol(vol);
	if(ret){
		zxc_env_set_vol(vol);
		player_volume_update();
	}
	return ret;
}

unsigned char ebm_env_set_ip(unsigned char *ip)
{
	unsigned int ip32, mask, gw;
	ip32= *((unsigned int*)ip);
	mask= *((unsigned int*)(ip+4));
	gw= *((unsigned int*)(ip+8));
	ip32= swap32(ip32);
	mask= swap32(mask);
	gw= swap32(gw);
	printf("ip= %x, mask =%x, gateway=%x\n", ip32, mask, gw);
	return 1;
}

unsigned int ebm_env_get_sip(void)
{
    return ReadCfg->ebm.plat_ip;
}


unsigned char ebm_env_set_platip(unsigned char *ip, unsigned short port)
{
#define usercfg_set_platip(val) set_val_u32(val, &p_wcfg->ebm.plat_ip)
#define usercfg_set_platport(val) set_val_u16(val, &p_wcfg->ebm.plat_port)  
	unsigned char ret;
	unsigned int ip_32= *((unsigned int*)ip);
	ip_32= swap32(ip_32);
	ret= usercfg_set_platip(ip_32);
	ret|= usercfg_set_platport(port);
	if(ret != 0){
		ebm_adt_command_start();
        ebm_adt_passback_start();
	}
	return 1;
}


unsigned char ebm_env_set_backip(unsigned char *ip, unsigned short port)
{
#define usercfg_set_backip(val) set_val_u32(val, &p_wcfg->ebm.passback_ip)
#define usercfg_set_backport(val) set_val_u16(val, &p_wcfg->ebm.passback_port)  
	unsigned char ret;
	unsigned int ip_32= *((unsigned int*)ip);
	ip_32= swap32(ip_32);
	ret= usercfg_set_backip(ip_32);
	ret|= usercfg_set_backport(port);
	if(ret != 0){
		ebm_adt_command_start();
        ebm_adt_passback_start();
	}
	return 1;
}

#define MAX_TP (60*10)
unsigned char ebm_env_set_back_tp(unsigned short val)
{
#define usercfg_set_passback_tp(val) set_val_u16(val, &p_wcfg->ebm.passback_tp)      
	if(val > MAX_TP)
		val= MAX_TP;
	return usercfg_set_passback_tp(val);
}

unsigned short ebm_env_get_back_tp(void)
{
	return ReadCfg->ebm.passback_tp;
}

unsigned char ebm_env_set_heart_tp(unsigned short val)
{
#define usercfg_set_heart_tp(val) set_val_u16(val, &p_wcfg->ebm.heart_tp)      
    if(val > MAX_TP)
        val= MAX_TP;
    return usercfg_set_heart_tp(val);
}

unsigned short ebm_env_get_heart_tp(void)
{
    return ReadCfg->ebm.heart_tp;
}


unsigned char ebm_env_set_fm_tp(unsigned short val)
{
#define usercfg_set_fm_tp(val) set_val_u8(val, &p_wcfg->ebm.fm_tp)      
    return usercfg_set_fm_tp(val);
}


unsigned char ebm_env_set_sourceid(unsigned char *sourceid)
{
#define usercfg_set_sourceid(val) set_val_array(p_wcfg->ebm.source_id, val, 12);
extern void zxc_env_set_logid(unsigned char *logid, unsigned int len);
	unsigned char ret;
	sourceid[0]&=0xf;
	ret= usercfg_set_sourceid(sourceid);
	if(ret){
		printf("sourceid restart network\n");
		command_net_restart();
		passback_net_restart();
		zxc_env_set_logid(sourceid, 12);
	}
	return ret;
	
}

unsigned char ebm_env_get_sourceid(unsigned char *buf, unsigned char buf_len)
{
	if(buf_len != 12 || buf == NULL)
		return 0;
	memcpy(buf, ReadCfg->ebm.source_id, 12);
	buf[0] |= 0xf0;
	return 1;
}

unsigned char ebm_env_set_trumpet(unsigned char val)
{
#define usercfg_set_flgAmplifier(val) set_val_u8(val, &p_wcfg->flgAmplifier)      
	return usercfg_set_flgAmplifier(val);
}

unsigned char ebm_env_get_trumpet(void)
{
	return ReadCfg->flgAmplifier;
}


unsigned char ebm_env_set_systime(unsigned int val)
{
extern unsigned char ebm_time_handle(unsigned int utc_t);
	ebm_time_handle(val);
	return 1;
}


unsigned char ebm_env_get_register_1st_time(void)
{
	return 2;
}

static unsigned int _convert_version_str(const char *str)
{
	const char *cp;
	unsigned int data, bdata;

	for (cp = str, data = 0; *cp != 0; ++cp) {
		if (*cp >= '0' && *cp <= '9')
			bdata = *cp - '0';
		else if (*cp >= 'A' && *cp <= 'F')
			bdata = *cp - 'A' + 10;
		else if (*cp >= 'a' && *cp <= 'f')
			bdata = *cp - 'a' + 10;
		else if ('.' == *cp)
			continue;
		else
			break;
		data = (data << 4) | bdata;
	}
	return data;
}


unsigned char ebm_env_get_phyid(unsigned char *id, unsigned char len)
{
	unsigned char ret=0;
	if(len == 6 && id != NULL){
		ret = ebm_get_phyid(id);
	}
	return ret;
}

unsigned char ebm_env_get_playsta(void)
{
	enum{
		IDLE=1,
		WORKING=2,
		ERR=3,
	}play_sta;
	play_sta= IDLE;
	if(1 == ebm_rtp_visit_netsta(NULL, NULL) && is_current_pg_vaild()){
		play_sta= WORKING;
	}
	return play_sta;
}


unsigned int oem_get_manufacture(void)
{
	static unsigned int val=0;
	static unsigned char frist_run=1;
	unsigned int ret =0xffffffff;
	if(frist_run){
		char* p_str;
		p_str = gp_oemManufactureid;
		if(p_str){
			ret = (unsigned int)atoi(p_str);
		}
		val=ret;
		frist_run=0;
	}
	else
		ret= val;
	return ret;
}

unsigned int oem_get_hard_ver(void)
{
	static unsigned int val=0;
	static unsigned char frist_run=1;
	unsigned int ret =0xffffffff;
	if(frist_run){
		char* p_str;
		p_str = gp_oemHardwareVersion;
		if(p_str){
			ret = _convert_version_str(p_str);
		}
		val=ret;
		frist_run=0;
	}
	else
		ret= val;
	return ret;
}

unsigned int oem_get_flashoem_ver(void)
{
	unsigned int ret =0xffffffff;
	char* p_str;
	p_str = gp_oemSoftversion;
	if(p_str){
		ret = _convert_version_str(p_str);
	}
	return ret;
}

unsigned int oem_get_soft_ver(void)
{
//extern const unsigned int soft_version;
	return 0;
}


unsigned int app_get_systime(void) {
    return (unsigned int)getNowTime();
}



